package com.young.ums.service.impl;

import com.young.common.exception.BaseException;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.ums.core.ETopic;
import com.young.ums.core.ISubscriber;
import com.young.ums.model.NavigateButton;
import com.young.ums.service.INavigateButtonService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
* 导航按钮关联信息服务实现
* Created by imrookie on 2018-10-28.
*/
@Service("navigateButtonService")
public class NavigateButtonServiceImpl implements INavigateButtonService,ISubscriber {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(NavigateButtonServiceImpl.class);

    private static final String MAPPER = "com.young.ums.mapper.navigateButton";//mapper的namespace

    //获取数据列表
    @Log("查询资源按钮关系列表")
    @Override
    public List<NavigateButton> getList(NavigateButton obj) {
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Log("分页查询资源按钮关系列表")
    @Override
    public List<NavigateButton> getPage(NavigateButton obj, Page page) {
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    //查询单条
    @Log("查询资源按钮关系")
    @Override
    public NavigateButton get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(NavigateButton obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(NavigateButton obj) {
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Log("批量删除资源按钮关系")
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    /**
     * 根据导航id和按钮id删除关联关系
     *
     * @param navigateId 导航id
     * @param buttonId   按钮id
     * @return
     */
    @Log("根据导航id和按钮id删除关联关系")
    @Override
    public int deleteByNavigateIdAndButtonId(String navigateId, String buttonId) {
        if (StringUtils.isBlank(navigateId) || StringUtils.isBlank(buttonId)){
            throw new BaseException("删除失败:导航ID和按钮ID不允许为空");
        }
        NavigateButton navigateButton = new NavigateButton();
        navigateButton.setNavigateId(navigateId);
        navigateButton.setButtonId(buttonId);
        return dataAccessService.delete(MAPPER + ".deleteByNavigateIdAndButtonId", navigateButton);
    }

    //删除单条
    @Log("删除资源按钮关系")
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    /**
     * 根据按钮id批量删除
     *
     * @param buttonId
     * @return
     */
    @Log("根据按钮id批量删除")
    @Override
    public int deleteByButtonId(String buttonId) {
        return dataAccessService.delete(MAPPER + ".deleteByButtonId", buttonId);
    }

    /**
     * 根据资源id批量删除
     *
     * @param navigateId
     * @return
     */
    @Log("根据资源id批量删除")
    @Override
    public int deleteByNavigateId(String navigateId) {
        return dataAccessService.delete(MAPPER + ".deleteByNavigateId", navigateId);
    }

    //插入
    @Log("插入资源按钮关系")
    @Override
    public int insert(NavigateButton obj) {
        if (StringUtils.isBlank(obj.getId())){
            obj.setId(CommonUtil.getUUID());
        }
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Log("批量插入资源按钮关系")
    @Override
    public int batchInsert(List<NavigateButton> list) {
        int i = 0;
        for(NavigateButton item : list){
            i += this.insert(item);
        }
        return i;
    }

    /**
     * 根据导航id查询关联的按钮id
     *
     * @param navigateId 导航id
     * @return
     */
    @Log("根据导航id查询关联的按钮id")
    @Override
    public List<String> getButtonIdsByNavigateId(String navigateId) {
        return dataAccessService.getList(MAPPER + ".getButtonIdsByNavigateId", navigateId);
    }

    /**
     * 根据按钮id查询关联的导航id
     *
     * @param buttonId 按钮id
     * @return
     */
    @Log("根据按钮id查询关联的导航id")
    @Override
    public List<String> getNavigateIdsByButtonId(String buttonId) {
        return dataAccessService.getList(MAPPER + ".getNavigateIdsByButtonId", buttonId);
    }

    /**
     * 批量关联按钮到导航
     *
     * @param navigateId
     * @param buttonIds
     */
    @Log("批量关联按钮到导航")
    @Override
    public int relevanceButton2Navigate(String navigateId, String[] buttonIds) {
        int i = 0;
        if (StringUtils.isNotBlank(navigateId) && buttonIds != null && buttonIds.length > 0){
            for (String buttonId : buttonIds){
                //判断是否已经存在关联
                if (this.isRelevanced(navigateId, buttonId)){
                    logger.info("[批量关联按钮到导航] 导航[{}]与按钮[{}]已经存在关联关系,跳过.", navigateId, buttonId);
                    continue;
                }
                NavigateButton navigateButton = new NavigateButton();
                navigateButton.setNavigateId(navigateId);
                navigateButton.setButtonId(buttonId);
                i += this.insert(navigateButton);//插入关联关系
            }
        }
        return i;
    }

    /**
     * 判断是否存在关联关系
     *
     * @param navigateId 导航id
     * @param buttonId   按钮id
     * @return
     */
    @Log("判断是否存在关联关系")
    @Override
    public boolean isRelevanced(String navigateId, String buttonId) {
        NavigateButton navigateButton = new NavigateButton();
        navigateButton.setNavigateId(navigateId);
        navigateButton.setButtonId(buttonId);
        return this.count(navigateButton) > 0;
    }

    /**
     * 返回订阅的主题数组
     */
    @Override
    public ETopic[] topics() {
        return new ETopic[]{ETopic.BUTTON_DEL, ETopic.NAVIGATE_DEL};
    }

    /**
     * 消费
     *
     * @param topic   主题
     * @param message 传输对象
     */
    @Override
    public void consume(ETopic topic, Object message) {
        try {
            if (topic == ETopic.BUTTON_DEL){
                this.deleteByButtonId(message.toString());
            }else if (topic == ETopic.NAVIGATE_DEL){
                this.deleteByNavigateId(message.toString());
            }
        } catch (Exception e) {
            logger.info("[UMS消息消费-NavigateButtonServiceImpl] 消费异常,主题={},消息={}", topic, message, e);
            throw new BaseException(e);
        }
    }
}